A clustering-optimized segmentation algorithm and application on food quality detection

For solving the problem of quality detection in the production and processing of stuffed food, this paper suggests a small neighborhood clustering algorithm to segment the frozen dumpling image on the conveyor belt, which can effectively improve the qualified rate of food quality. This method builds feature vectors by obtaining the image's attribute parameters. The image is segmented by a distance function between categories using a small neighborhood clustering algorithm based on sample feature vectors to calculate the cluster centers. Moreover, this paper gives the selection of optimal segmentation points and sampling rate, calculates the optimal sampling rate, suggests a search method for optimal sampling rate, as well as a validity judgment function for segmentation. Optimized small neighborhood clustering (OSNC) algorithm uses the fast frozen dumpling image as a sample for continuous image target segmentation experiments. The experimental results show the accuracy of defect detection of OSNC algorithm is 95.9%. Compared with other existing segmentation algorithms, OSNC algorithm has stronger anti-interference ability, faster segmentation speed as well as more efficiently saves key information ability. It can effectively improve some disadvantages of other segmentation algorithms.

A clustering-optimized segmentation algorithm and application on food quality detection

QingE Wu * , Penglei Li , Zhiwu Chen & Tao Zong
For solving the problem of quality detection in the production and processing of stuffed food, this paper suggests a small neighborhood clustering algorithm to segment the frozen dumpling image on the conveyor belt, which can effectively improve the qualified rate of food quality. This method builds feature vectors by obtaining the image's attribute parameters. The image is segmented by a distance function between categories using a small neighborhood clustering algorithm based on sample feature vectors to calculate the cluster centers. Moreover, this paper gives the selection of optimal segmentation points and sampling rate, calculates the optimal sampling rate, suggests a search method for optimal sampling rate, as well as a validity judgment function for segmentation. Optimized small neighborhood clustering (OSNC) algorithm uses the fast frozen dumpling image as a sample for continuous image target segmentation experiments. The experimental results show the accuracy of defect detection of OSNC algorithm is 95.9%. Compared with other existing segmentation algorithms, OSNC algorithm has stronger anti-interference ability, faster segmentation speed as well as more efficiently saves key information ability. It can effectively improve some disadvantages of other segmentation algorithms.

FCM
Fuzzy C-means clustering algorithm K-Means K-means clustering algorithm PSO-FCM Particle swarm optimization-fuzzy C-means clustering algorithm OSNC Optimized small neighborhood clustering algorithm VOC Visual object class dataset For effective segmentation so as to improve product qualification of defect targets, some hidden defect features, such as cracks, breakages, stains and so on need to be carried out detect early from images of food products containing fillings, which can greatly improve the recognition rate of dumpling defects, thus the quality of dumplings is approved. The image analysis of filled food products has become important with advances in artificial intelligence and increasing labor costs. The basis for image segmentation target recognition, matching and tracking were important for image understanding, image analysis, pattern recognition, computer vision and others 1 . The result of image segmentation can segment a food processing scene image into target regions, thus providing the location of the target in the image. Algorithms based on grey-scale threshold segmentation 2 , edge segmentation 3 and region segmentation 4 are widely used in image segmentation. The threshold segmentation method is particularly suitable for images where the target and background occupy different grey level ranges and has been applied in many fields, where the selection of threshold values is a key technique in image threshold 5,6 . The edge information is the detailed information when the grey scale of the image changes 7 . In order to segment out the region of interest, there were other edge detection operators such as Sobel et al. 8 . The processing principle of such edge detection algorithms was used to record the grey jump, and when the grey jump matches the set threshold, the edge features are extracted using the difference operation method 9 . It has been shown that both segmentation methods are widely used. Yang 10 proposed a supervised multiple threshold segmentation models to complete the detection of potato sprouting. In addition, scholars have also actively improved the edge detection operator. Lu 11 introduced a threshold selection method based on the local maximum inter-class variance algorithm in the Canny edge detection algorithm in order to improve the efficiency of thermal image recognition. Liao 12 used a supervised block-based region segmentation algorithm to segment tumor regions from breast ultrasound images, combined with a deep learning network, in order to predict whether a breast tumor is benign or malignant.
The cluster segmentation is one of the specific theoretical approaches to image, typically, the K-means clustering algorithm 13 and the fuzzy C-mean clustering algorithm 14 . Trivedi 15 used a K-means clustering segmentation algorithm to segment plant leaves into homogeneous segments which significantly improved the accuracy of plant leaf pest detection. Wu 16 used the Canny algorithm to process text image edge detection and then used the k-means algorithm for clustering pixel recognition, which effectively improved the accuracy of text image recognition. Fuzzy C-Means (FCM) was the most useful image segmentation algorithm for realistic scenarios 17 . The FCM segmentation algorithm deals directly with the greyscale image by using fuzzy theory. The purpose of the clustering operation carried out classifying the dataset more accurately and reasonably, classifying all samples with similar features. Some samples with more different features could be classified in different categories so as to reach the most reasonable segmentation effect 18 . Gao 19 proposed a robust fuzzy c-mean clustering method based on the adaptive elastic distance for image segmentation. Brikh 20 combined fuzzy C-means and particle swarm optimization (PSO) algorithms to cluster large nonlinear data sets.
FCM clustering algorithm and K-means segmentation algorithm were well applied in Various image segmentation practices 21 . However, they also suffer from the following disadvantages, such as searching time of both types of algorithms and their derivatives is longer especially multi-threshold segmentation. The larger the image size is, the longer the segmentation time is 22 . The parameters need to be set, and the optimal number of partitions could not be obtained by existing methods 23 . In addition, because the defects of stuffed food are relatively small to obtain feature of defects difficultly. it is necessary to establish an algorithm suitable for this defect to realize dumpling image segmentation. This paper proposes an optimized small neighborhood clustering (OSNC) segmentation algorithm, which implements the segmentation of stuffed food, and verifies the effectiveness of the algorithm by using the open-source datasets.
To verify the feasibility of the OSNC segmentation algorithm for fast and accurate segmentation of images of stuffed food in real production. Based on that, a Matlab defect detection platform was built to detect defects in the production process of frozen dumplings. The processes are as follows: (1) a grey-scale camera is set up to capture the image information of the frozen dumplings; (2) the OSNC algorithm pre-processes the samples; and (3) the defect detection platform locates the defective dumplings. The specific flow chart is as follow in Fig. 1.

Small neighborhood clustering segmentation algorithm
Suppose n is classes of sample data, each of which is a set of data α 1 , α 2 , . . . , α n . Suppose that there are m attribute parameters of the sample, For example, the image peak, single peak, grayscale, valley, color, edge, inflection point, tone, multimodal and other indicators 24 . The m-dimensional feature space is constructed according to the indicators of these sample points, and all the sample points correspond to the points in the m-dimensional feature space.
Small neighborhood segmentation algorithm is given as follows: Step 1 For a given sample x = �c 1 (x), c 2 (x), . . . , c m (x)� , there are m attributes for sample x.
Step 3 By calculating the distance between any two samples in the sample space, there are K classes are obtained, that is closest to sample x , y 1 , y 1 , . . . , y K . y i represents one of the K.
Step 4 Regard class x as a center, an appropriate class are found in a small neighborhood with radius ε by clustering.
Step 5 For the sample x that needs to be segmented, d 1 , d 2 , · · · , d K between the K nearest neighbors x, y 1 , …, x, y K is defined by the distance.
Step 6 For L samples x , that is marked as x l , l = 1, 2, . . . , L , and n classes α i , i = 1, 2, . . . , n , there are N i samples in each class, and one sample has m attributes.
Step 7 Next, for K neighbors of sample x l , that is marked as x l+1 , . . . , x l+K , the center of single attributes is shown under the formula.
For sample x the same attribute of K that is marked as x l+1 , . . . , x l+K of is defined as follows:  The training procedures are as follows: P1 Configure the original iterative value of the algorithm to the same attribute, and set the sample number s to s = 0. P2 Then the circle center attribute is set to V q l . Compute the nearest neighbors of V q l in a small neighborhood of radius ε . When you get a proper nearest neighbor V , update the value of s : s = s + 1. P3 Search P2 in turn until the nearest neighbor V does not exist, s neighbors of V q l can be obtained. Then the number of samples of the same attribute is s . Then, the weight of each attribute of the sample can be defined as ζ q = s L,q = 1, 2, . . . , m . Define ζ p = max q∈{1,2,··· ,m} ζ q , Then ζ p is the known class. Combined with formula (1), the average value of the same attribute for different samples of each class is defined as follows: The training process is shown in Fig. 2. The segmentation procedures are as follows: P5 According to the above P4 search small neighborhood algorithm, simulation iteration, until the nearest neighbor V ′ does not exist. P6 Given z = z + 1.
At the same time, the number of obtained V q α i is stored as v and assigned to c h0 = max {c h0 , v} . Then, the weight of each attribute corresponding to the sample contained in each class can be defined as v N i and assigned to b hq = max b hq , v N i ,q = 1, 2, . . . , m , h = 1, 2, . . . , N i . P7 If v = c h0 , the iteration is terminated. Otherwise, P3 ~ P6 are continued until v = c h0 , and the segmentation ends. At this time, the number of total classes of the dataset to be segmented is z , and assigned to n = z . In this way, the dataset is first divided into n classes. At the same time, assigned to c 0 = c h0 and b q = b hq . www.nature.com/scientificreports/ Suppose that the data to be segmented has been determined n classes, and each class has m mean. The metric function d i of the distance function between the sample element x to be segmented and a certain type of element in the training sample is: Then the minimum values of these distances are obtained as shown in Formula (4): Then the formula (4) can determine which class α i * the sample x to be segmented belongs to. For the sample x to be segmented, the distances between x and n classes are respectively defined as d 1 , d 2 , . . . , d n . The calculation of decision weight can also be calculated by i as shown in Formula (5): From the obtained i * , which can also determine which class α i * the sample x to be segmented belongs to. The segmentation algorithm is shown in Fig. 3.

Optimistic method
Selection of optimal segmentation points. The pixel value of the grayscale image is used as the input of the algorithm to verify the effective segmentation algorithm. If the shape of the image is M × N , the corre- . . , N} are the set of distances between cluster centers. The objective function formula of segmentation center is: where r is the fuzzy weight index. There is: The calculation results of the segmentation center o k and the final value µ k (L ij ) of the membership matrix are shown in Formulas (8) and (9): Segmentation center can be calculated quickly by initial membership matrix and formula (8). Then calculate the new value of µ k (L ij )(∀k, i, j) by o k and formula (9). After many calculations, until µ k (L ij )(∀k, i, j) is stable. Define O as the final set of segmentation centers and use the following formula to calculate the image segmentation threshold: www.nature.com/scientificreports/ where, G is the number of thresholds, β and β are the weight coefficients. satisfy the formula (11).
Usually select β =β = 0.5. This paper takes pictures in VOC database as segmentation samples. The above segmentation algorithm is used to segment the testing image with different thresholds, and the results are shown in Fig. 4.
Selection of optimal segmentation sampling rate. Usually, the fixed interval algorithm for image information acquisition does not have much impact on the image processing results, and can save equipment memory. Therefore, most image processing algorithms will resample the image. The resampling algorithm can be described as formula (12): The value range of resampling rate is 0 < η < 1 , the coordinate of initial image is (x 0 , y 0 ) , and it is (x 1 , y 1 ) after formula transformation. The new data generated is related to the value of η . When the value of η is small, the information acquisition effect is good, but the image distortion is obvious, and important information is lost. Therefore, selecting appropriate proportion is the key to effective segmentation. Selecting the appropriate sampling rate can make the information loss acceptable, which is a feasible algorithm. The information calculation referred to the segmentation method based on histogram entropy.
Calculation of optimal sampling rate. The algorithm proposed in this paper uses entropy loss information as the standard to evaluate the distortion degree of the image. On this basis, in order to achieve good segmentation effect, the relative entropy loss degree is used as the selection basis of sampling rate in sampling. When the sample image has enough segmentation information, the sample image information is used to calculate the segmentation threshold. The obtained sample image is similar to the histogram shape of the original image, that is, the information of the sample image is basically the same as that of the original image. Figure 5 shows the sample image and its histogram at different sampling rates.
According to Fig. 5, the histogram shape of the sample image 25 is basically the same under different resampling rates, indicating that the resampled image retains most of the information of the original image; But histograms differ from each other. When the sampling rate decreases, the image information is lost, and the curves in each histogram change obviously, which indicates that the accuracy of segmentation can be guaranteed by obtaining appropriate sampling rate.
The definition of Shannon entropy is shown in formula (13): (10) J c = βo c +βo c+1 , c = 1, 2, . . . , G (11) β +β = 1 (12)   (14): Define P k as: where, C is the sum of grayscale levels, and R(i, j) is the grayscale value. P k satisfies the following: Relative entropy loss can measure the degree of information loss. Suppose that the entropy of the sample image is S 1 , the entropy of the sample image is S η when the sampling rate is η , the relative entropy loss is as follows: It can be seen from the above analysis that the relative entropy loss can be used as the basis for the selection of sampling rate. In order to explore the relationship between them, this paper analyzes the change trend of relative entropy loss in the range of sampling rate η ∈ [0.01, 0.9] , and the trend curve is shown in Fig. 6.   Figure 6d shows that δ η increases when η decreases. This trend shows that when η decreases, the sample image distortion increases, but the distortion is small. Most of the original information remains within a certain sampling rate. When the sampling rate is very small, δ η will greatly increase. Within the allowable range of relative entropy loss, the image has less data at the minimum sampling rate and the thresholds calculated are also reliable. In this range, the minimum sampling rate can be calculated by searching the optimal sampling rate algorithm.
Search for optimal sampling rate. The minimum sampling rate can be calculated by dichotomy. Although this algorithm is proved to be effective, it needs more iterations. In order to improve the search efficiency, variable step search can be used to find the minimum sampling rate. Suppose the relative entropy loss range is [δ min , δ max ] , the optimal sampling rate η o is: In fact, η o cannot be calculated accurately. It is unnecessary to continuously search η o for the accuracy of this paper. Therefore, this paper selects the first sampling rate η f instead of η o to meet the constraint of relative entropy loss. Suppose that the current iterative search step is t , the variable step search algorithm is as follows: For a single target image, the number of sample image datasets with sampling rate η f is limited. Therefore, the histogram created cannot contain data for each class, which affects the single peak judgment. To calculate the optimal number of thresholds, use the size M × N of image S 0 to ensure that the sampling rate is within the optimal range. Therefore, the optimal sampling rate η o can be defined as: Set the number of optimization steps H , the number of sample classes (N + 1) , the number of class separation distance k , so the complexity of the algorithm θ x is as follows:  www.nature.com/scientificreports/ Judgment function of validity in segmentation. In this section, in order to find out the optimal segmentation number of images, an improved correlation function between fuzzy sets is constructed. This function is used to judge the effectiveness of image segmentation 26 . In fuzzy partition, fuzzy membership describes the correlation of classification data sets. Suppose that the shape of the image is M × N , the corresponding set L = L ij , i = 1, 2, . . . , M, j = 1, 2, . . . , N of image grayscale value matrices containing α classes.
Then the fuzzy deviation degree for class c is: Define the fuzzy relation matrix set R kl as: The fuzzy membership of classes k and l is defined as: Then fuzzy membership function can be defined as validity judgment function. If the following equation is satisfied: Then α * is the optimal segmentation number of the sample image. Through the above analysis, the flow chart of the OSNC algorithm constructed in this paper is shown in Fig. 7.

Evaluation index
Establish a unified comparative value: Supposing that the number of testing samples in a single experiment is N and the number of correct detections is n i (i = 1, 2, . . . , N) , the single recognition accuracy rate M s is defined as follows according to the experimental situation: Suppose the number of experiments is P , the average recognition accuracy M is:

Experiment and result analysis
The experiment environment is Windows 10 operating system, and all the simulation experiments are run using a CPU of Intel(R) Core(TM) i7-9700, a 4-core processor at 3.0 GHz, 32.0 GB RAM. In order to verify the effectiveness of the segmentation validity judgment function. Taking Fig. 6a as the segmentation sample, the value of the segmentation validity judgment function is calculated. The experimental comparison results are shown in Table 1.
As shown in Table 1, the OSNC method has fewer iterations and shorter searching time than the other methods (K-means 15 , FCM 21 , PSO-FCM 20 ). Experiments show that this method reduces the search time of the optimal segmentation number to a certain extent. The results of the four image segmentation methods are shown in Fig. 8.
This paper uses the sample images in the VOC database to verify the feasibility of the algorithm through continuous image segmentation experiments. Next, for the image samples collected in the industrial production site of frozen dumplings, the effectiveness of the OSNC algorithm is verified by combining the Matlab image processing platform.

Comparison of OSNC algorithm and existing algorithms
Data source. In this paper, the effectiveness of the algorithm is verified by the field images data of the factory frozen dumpling production line. Sample images are sampled by grayscale camera. To ensure images quality, the resolution ratio of the camera reaches at least 2 million pixels. The camera is erected directly above the conveyor belt, and the receptive field cannot exceed the maximum edge of the conveyor belt. The camera samples every 0.15 s. Image samples include positive samples (qualified dumplings) and negative samples (defective dump- Table 1. Comparison results of iteration times and searching time between this paper and the other algorithm. www.nature.com/scientificreports/ lings), and normalized to the same size. Sample images captured under different background colors (dark-green and white) are shown in Fig. 9.
Experiment. The hardware environment of the experiment is Windows 10 operating system, and all the simulation experiments are run using a CPU of Intel(R) Core(TM) i7-9700, a 4-core processor at 3.0 GHz, 32.0 GB RAM. The software of this experiment is a YOLOv3 defect detection platform based on Matlab, and the OSNC image segmentation algorithm is added after the input and before the backbone network. In this experiment, 4000 images of frozen dumplings were used as sample databases, including 2000 images of dark-green background and 2000 images of white background. The database is divided into training samples and testing samples according to the ratio of 1:1.  www.nature.com/scientificreports/ fails to segment the cracked defects, or is considered to be insensitive to changes in the grey value at the cracked defects, and uses the folds of the dumpling skin as key information for segmentation. the FCM segmentation algorithm is too sensitive to changes in the grey value of the overall image, and the segmentation contains both key and noisy information; PSO-FCM can effectively remove the background interference, but it will retain most of the defect information and redundant information at the same time. The PSO-FCM segmentation effect is better than the original FCM segmentation algorithm; the OSNC algorithm proposed in this paper is effective in segmenting the cracked dumplings for The OSNC algorithm proposed in this paper can effectively segment the key information of the dumpling cracks and is almost unaffected by noise, which provides a good preparation for subsequent defect detection. In Fig. 11, all four segmentation algorithms can effectively segment the background, defects and dumpling wrapper. However, in terms of segmentation effectiveness, the OSNC algorithm in this paper has significant background noise reduction, can retain the key dumpling defect features, and is highly resistant to interference. P3: The training sample images processed by K-means, FCM, PSO-FCM and OSNC segmentation algorithms are respectively imported into Matlab image processing platform. After the model training is stable, the corresponding four models are recorded as: K-means, FCM, PSO-FCM and OSNC. The platform uses fast convolution network combined with edge detection algorithm for feature extraction. The dumplings that do not meet the production requirements such as surface damage, crack and stain can be identified and framed, and the label is defined as "Bad". For qualified dumplings can also be identified and framed, define the label as "Good".
P4: Use testing sample images to test the defect detection effect of frozen dumplings. For the same test sample images, the visual detection results of the four models are shown in Fig. 12. According to Fig. 12, using the model of K-means, FCM and PSO-FCM segmentation algorithm, there are some misjudgments in dumpling defect detection, as shown in the red box in the Fig. 12. In contrast, the model using OSNC algorithm can accurately identify qualified and unqualified dumplings, and has stronger anti-interference ability and higher confidence level.
P5: Evaluate four defect detection methods. Four defect detection models obtained by P3 were used to detect all test sample images. 500 experiments were conducted respectively. The experiment records the detection time and the results of each model (the number of correct recognition and error recognition).

Result analysis.
In the experiment, the recognition accuracy rate is calculated every 50 times, and the comparison results are shown in Fig. 13. The total number of samples, the number of accurately detected samples and the detection accuracy rate can be obtained from the Figure at any time. As shown in the Figure, with the increase of sample size, the accuracy of dumpling defect detection increases. After reaching a certain sample size, the curve tends to be stable. After 500 experiments, the accuracy rates of defect detection of frozen dumplings using the models of OSNC, PSO-FCM, FCM and K-means algorithms were 95.9%, 92.5%, 90.2% and 87.5%, respectively. The experimental results show that the OSNC algorithm can not only improve the accuracy rate of model defect detection, but also shorten the detection time.
Comprehensive evaluation of the performance of four segmentation algorithms: (1) The segmentation time of the algorithm for segmentation samples. For the training samples of this experiment, it is the average time for the algorithm to segment samples 10 times. (2) Anti-interference capability of the algorithm. It is an approximate estimation based on the segmentation effect and algorithm complexity.  Table 2.
From the comparison of experimental results, the OSNC algorithm can quickly and accurately segment the frozen dumpling images. The image detection model using OSNC algorithm not only has fast processing speed, but also has higher recognition accuracy for frozen dumpling defects, which is more than 5% higher than that using other segmentation algorithm. The effectiveness of the algorithm established in this paper is proved.  www.nature.com/scientificreports/ In addition, the OSNC algorithm has strong anti-interference ability and better adaptability to different environments. In order to further improve the accuracy rate of defect detection, in actual processing and production, two cameras can be used to sample the image information of the dumplings on the conveyor belt. It is convenient for subsequent executing agencies to eliminate unqualified dumplings.  www.nature.com/scientificreports/

Discussion
In this paper, an OSNC segmentation algorithm is established to cluster the feature vectors of stuffed food images. The image is segmented by using the distance function between categories. In order to optimize the OSNC segmentation algorithm, this paper calculates the best segmentation point by constructing the objective function of the clustering segmentation center; the variable step search algorithm is introduced to optimize the time of calculating the minimum sampling rate and improve the segmentation speed. At the same time, the relative entropy loss is used as the basis for judging the image sampling distortion. In addition, the fuzzy correlation is also considered, and the validity judgment function of segmentation is obtained, and the optimal segmentation number can be calculated. This paper used the images in the VOC database to verify the feasibility of the algorithm, and used the frozen dumpling image to verify the effectiveness of the algorithm. According to the comparative experimental results, the OSNC algorithm has faster segmentation speed and stronger antiinterference ability. The defect detection accuracy rate of the image processing model using this algorithm is more than 95%, which is about 5% higher than that of the other algorithm, and the defect detection speed is faster. The application of this method can meet the factory 's detection and elimination of defective dumplings and improve the qualified rate of dumpling production. In order to enhance the rapidity and robustness, the small neighborhood algorithm will be improved through the aspects of objective function, membership function and distance function, in the future research.

Data availability
The data that support the findings of this study are available on request from the corresponding author. The data are not publicly available due to privacy or ethical restrictions.